package com.ourteam.workspace.dao.impl;

import com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO;
import com.ourteam.workspace.dao.WorkspaceProductDatabaseModelRelate;
import com.ourteam.workspace.dao.WorkspaceProductDatabaseModelRelateDAOQueryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;

/**
 * Template Name : DAO服务对象 Template Version : 1 Template Last Modfiy Date : Fri Nov 17 11:00:11 CST
 * 2017 Auto Generate Date : 2018-11-30 18:42:35 Warning ! This file will be overwrite when
 * regenerate !
 */
public class AbstractWorkspaceProductDatabaseModelRelateDAOJdbcImpl extends AbstractSpringDAO
    implements IWorkspaceProductDatabaseModelRelateDAO {

  public static final String[] ENTITY_FIELDS =
      new String[] {
        Id,
        WorkspaceProductDatabaseRelateId,
        ModelPcakageId,
        LastUpdateTime,
        SubmitterId,
        SubmitterName,
        Status,
        Remarks,
        PackageName,
        DomainId,
        DomainName
      };

  public static final String[] ENTITY_PROPERTIES =
      new String[] {
        _DataObjectProperty.Id,
        _DataObjectProperty.WorkspaceProductDatabaseRelateId,
        _DataObjectProperty.ModelPcakageId,
        _DataObjectProperty.LastUpdateTime,
        _DataObjectProperty.SubmitterId,
        _DataObjectProperty.SubmitterName,
        _DataObjectProperty.Status,
        _DataObjectProperty.Remarks,
        _DataObjectProperty.PackageName,
        _DataObjectProperty.DomainId,
        _DataObjectProperty.DomainName
      };

  public static final String[] ENTITY_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {};

  private static String relatedJoinSQL = null;

  private static String basicFilterSQL = null;

  private DataAccessObjectHelper<
          WorkspaceProductDatabaseModelRelate, WorkspaceProductDatabaseModelRelateDAOQueryBean>
      daoHelper;

  public AbstractWorkspaceProductDatabaseModelRelateDAOJdbcImpl() {
    super();
    daoHelper =
        new DataAccessObjectHelper<
            WorkspaceProductDatabaseModelRelate, WorkspaceProductDatabaseModelRelateDAOQueryBean>(
            this);
  }

  protected String getEntityName() {
    return EntityName;
  }

  protected String[] getEntityFields() {
    return ENTITY_FIELDS;
  }

  protected String[] getEntityProperties() {
    return ENTITY_PROPERTIES;
  }

  protected String[] getEntityPKFields() {
    return ENTITY_PK_FIELDS;
  }

  protected String[] getEntityAutoGenPKFields() {
    return ENTITY_AUTO_GEN_PK_FIELDS;
  }

  protected String[] getEntityUniqueFields() {
    return ENTITY_UNIQUE_FIELDS;
  }

  protected String getBaiscQueryFilter() {

    if (basicFilterSQL == null) {
      StringBuffer basicFilterSQLbuffer = new StringBuffer();

      basicFilterSQL = basicFilterSQLbuffer.toString();
    }

    return basicFilterSQL;
  }

  protected String getRelatedJoinSQL() {

    if (relatedJoinSQL == null) {
      StringBuffer relatedJoinSQLBuffer = new StringBuffer();

      relatedJoinSQLBuffer
          .append(" LEFT JOIN ")
          .append(com.ourteam.product.model.dao.IProductModelPackageDAO.EntityName)
          .append(" RELATED_PC6QLI_ProductModelPackage ");

      relatedJoinSQLBuffer.append(" ON ( ");

      relatedJoinSQLBuffer.append(
          StringUtils.replace(
              com.ourteam.product.model.dao.IProductModelPackageDAO.Id,
              com.ourteam.product.model.dao.IProductModelPackageDAO.EntityName + ".",
              "RELATED_PC6QLI_ProductModelPackage."));
      relatedJoinSQLBuffer.append(" = ").append(ModelPcakageId);

      relatedJoinSQLBuffer.append(" ) ");

      relatedJoinSQLBuffer
          .append(" LEFT JOIN ")
          .append(com.ourteam.product.model.dao.IProductModelDomainDAO.EntityName)
          .append(" RELATED_F32XVH_ProductModelDomain ");

      relatedJoinSQLBuffer.append(" ON ( ");

      relatedJoinSQLBuffer.append(
          StringUtils.replace(
              com.ourteam.product.model.dao.IProductModelDomainDAO.Id,
              com.ourteam.product.model.dao.IProductModelDomainDAO.EntityName + ".",
              "RELATED_F32XVH_ProductModelDomain."));
      relatedJoinSQLBuffer.append(" = ").append(DomainId);

      relatedJoinSQLBuffer.append(" ) ");

      relatedJoinSQL = relatedJoinSQLBuffer.toString();
    }

    return relatedJoinSQL;
  }

  /**
   * Check Data Unique
   *
   * @param aWorkspaceProductDatabaseModelRelate
   * @throws Exception
   */
  public boolean checkIsUnique(
      WorkspaceProductDatabaseModelRelate aWorkspaceProductDatabaseModelRelate) throws Exception {

    if (aWorkspaceProductDatabaseModelRelate == null) {
      return true;
    }

    List valueList = new ArrayList();

    List pkValueList = new ArrayList();

    pkValueList.add(aWorkspaceProductDatabaseModelRelate.getId());

    return isUnique(
        valueList.toArray(new Object[valueList.size()]),
        pkValueList.toArray(new Object[pkValueList.size()]));
  }

  /**
   * Insert Data
   *
   * @param aWorkspaceProductDatabaseModelRelate
   * @throws Exception
   */
  public void insert(WorkspaceProductDatabaseModelRelate aWorkspaceProductDatabaseModelRelate)
      throws Exception {

    super.doInsert(aWorkspaceProductDatabaseModelRelate);
  }

  /**
   * Batch Insert Datas
   *
   * @param aWorkspaceProductDatabaseModelRelate
   * @throws Exception
   */
  public void batchInsert(
      WorkspaceProductDatabaseModelRelate[] aWorkspaceProductDatabaseModelRelates)
      throws Exception {

    super.doBatchInsert(aWorkspaceProductDatabaseModelRelates);
  }

  /**
   * Delete Data
   *
   * @param final long id *
   * @throws Exception
   */
  public void delete(final long id) throws Exception {

    super.doDeleteById(new Object[] {new Long(id)});
  }

  /**
   * Delete Datas
   *
   * @param queryBean
   * @throws Exception
   */
  public void delete(WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean) throws Exception {

    super.doDeleteByQuery(queryBean);
  }

  public void delete(WorkspaceProductDatabaseModelRelate aWorkspaceProductDatabaseModelRelate)
      throws Exception {

    delete(aWorkspaceProductDatabaseModelRelate.getId());
  }

  /**
   * Update Data Selective
   *
   * @param aWorkspaceProductDatabaseModelRelate
   * @throws Exception
   */
  public void update(WorkspaceProductDatabaseModelRelate aWorkspaceProductDatabaseModelRelate)
      throws Exception {

    if (aWorkspaceProductDatabaseModelRelate.getId() == 0) {
      throw new Exception("Can not update object data whith out primary key value");
    }

    super.doUpdate(aWorkspaceProductDatabaseModelRelate);

    WorkspaceProductDatabaseModelRelate tempObj =
        findById(aWorkspaceProductDatabaseModelRelate.getId());

    if (tempObj != null) {
      PropertyUtils.copyProperties(aWorkspaceProductDatabaseModelRelate, tempObj);
    }
  }

  /**
   * Batch Update Datas
   *
   * @param aWorkspaceProductDatabaseModelRelates[]
   * @throws Exception
   */
  public void batchUpdate(
      WorkspaceProductDatabaseModelRelate[] aWorkspaceProductDatabaseModelRelates)
      throws Exception {

    super.doBatchUpdate(aWorkspaceProductDatabaseModelRelates);
  }

  /**
   * Update Data By Condictions
   *
   * @param aWorkspaceProductDatabaseModelRelate
   * @param aValueCriteria
   * @param aTransaction
   * @throws Exception
   */
  public void update(
      WorkspaceProductDatabaseModelRelate aWorkspaceProductDatabaseModelRelate,
      WorkspaceProductDatabaseModelRelateDAOQueryBean aQueryBean)
      throws Exception {

    super.doUpdateByQuery(aWorkspaceProductDatabaseModelRelate, aQueryBean);
  }

  /**
   * Update Data By Condictions
   *
   * @param aQueryBean
   * @throws Exception
   */
  public void update(WorkspaceProductDatabaseModelRelateDAOQueryBean aQueryBean) throws Exception {
    super.doUpdateByQuery(aQueryBean);
  }

  protected void checkNullValue(
      WorkspaceProductDatabaseModelRelate aWorkspaceProductDatabaseModelRelate) throws Exception {}

  /**
   * Get Data By ID
   *
   * @param final long id * @return ${businessObject.name}
   * @throws Exception
   */
  public WorkspaceProductDatabaseModelRelate findById(final long id) throws Exception {

    WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean =
        new WorkspaceProductDatabaseModelRelateDAOQueryBean();

    WorkspaceProductDatabaseModelRelateDAOQueryBean.Criteria criteria = queryBean.createCriteria();

    criteria.andIdEqualTo(id);

    // queryBean.setDistinct(true);

    WorkspaceProductDatabaseModelRelate[] WorkspaceProductDatabaseModelRelates = query(queryBean);

    if (WorkspaceProductDatabaseModelRelates.length == 0) {
      return null;
    } else {
      return WorkspaceProductDatabaseModelRelates[0];
    }
  }

  /**
   * Query Data
   *
   * @param queryBean
   * @return WorkspaceProductDatabaseModelRelate[]
   * @throws Exception
   */
  public WorkspaceProductDatabaseModelRelate[] query(
      WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean) throws Exception {

    List result = super.doQuery(queryBean, new JdbcWorkspaceProductDatabaseModelRelateRowMapper());

    return (WorkspaceProductDatabaseModelRelate[])
        result.toArray(new WorkspaceProductDatabaseModelRelate[result.size()]);
  }

  /**
   * Query ${businessObject.name} Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] querySelective(
      WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean) throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {
      queryBean.addAllSelectProperties();
    }

    return super.doQuerySelective(queryBean);
  }

  /**
   * Query Data Count
   *
   * @param queryBean
   * @return int
   * @throws Exception
   */
  public int queryCount(WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean)
      throws Exception {

    return super.doQueryCount(queryBean);
  }

  public void synchronize(
      WorkspaceProductDatabaseModelRelate[] aWorkspaceProductDatabaseModelRelates,
      WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean)
      throws Exception {
    daoHelper.synchronize(aWorkspaceProductDatabaseModelRelates, queryBean);
  }

  public void synchronize(
      WorkspaceProductDatabaseModelRelate[] aWorkspaceProductDatabaseModelRelates,
      WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean,
      net.dao.IDataAccessInterface.ISynchronizeCallback<WorkspaceProductDatabaseModelRelate>
          callBack)
      throws Exception {
    daoHelper.synchronize(aWorkspaceProductDatabaseModelRelates, queryBean, callBack);
  }

  public static class JdbcWorkspaceProductDatabaseModelRelateRowMapper
      implements RowMapper<WorkspaceProductDatabaseModelRelate> {

    public WorkspaceProductDatabaseModelRelate mapRow(ResultSet resultSet, int arg1)
        throws SQLException {
      WorkspaceProductDatabaseModelRelate object = new WorkspaceProductDatabaseModelRelate();

      int columnIndex = 1;

      int __dataType;

      try {

        Object id = null;

        id = resultSet.getObject(columnIndex++);

        object.setIdNull(resultSet.getBoolean(columnIndex++));

        if (id != null) {

          setBeanValue(object, _DataObjectProperty.Id, id);
        }

        Object workspaceProductDatabaseRelateId = null;

        workspaceProductDatabaseRelateId = resultSet.getObject(columnIndex++);

        object.setWorkspaceProductDatabaseRelateIdNull(resultSet.getBoolean(columnIndex++));

        if (workspaceProductDatabaseRelateId != null) {

          setBeanValue(
              object,
              _DataObjectProperty.WorkspaceProductDatabaseRelateId,
              workspaceProductDatabaseRelateId);
        }

        Object modelPcakageId = null;

        modelPcakageId = resultSet.getObject(columnIndex++);

        object.setModelPcakageIdNull(resultSet.getBoolean(columnIndex++));

        if (modelPcakageId != null) {

          setBeanValue(object, _DataObjectProperty.ModelPcakageId, modelPcakageId);
        }

        Object lastUpdateTime = null;

        __dataType = resultSet.getMetaData().getColumnType(columnIndex);

        if (__dataType == java.sql.Types.DATE
            || __dataType == java.sql.Types.TIME
            || __dataType == java.sql.Types.TIMESTAMP) {
          lastUpdateTime = resultSet.getTimestamp(columnIndex++);

        } else {
          lastUpdateTime = resultSet.getObject(columnIndex++);
        }

        object.setLastUpdateTimeNull(resultSet.getBoolean(columnIndex++));

        if (lastUpdateTime != null) {

          setBeanValue(object, _DataObjectProperty.LastUpdateTime, lastUpdateTime);
        }

        Object submitterId = null;

        submitterId = resultSet.getObject(columnIndex++);

        object.setSubmitterIdNull(resultSet.getBoolean(columnIndex++));

        if (submitterId != null) {

          setBeanValue(object, _DataObjectProperty.SubmitterId, submitterId);
        }

        Object submitterName = null;

        submitterName = resultSet.getObject(columnIndex++);

        object.setSubmitterNameNull(resultSet.getBoolean(columnIndex++));

        if (submitterName != null) {

          setBeanValue(object, _DataObjectProperty.SubmitterName, submitterName);
        }

        Object status = null;

        status = resultSet.getObject(columnIndex++);

        object.setStatusNull(resultSet.getBoolean(columnIndex++));

        if (status != null) {

          setBeanValue(object, _DataObjectProperty.Status, status);
        }

        Object remarks = null;

        remarks = resultSet.getObject(columnIndex++);

        object.setRemarksNull(resultSet.getBoolean(columnIndex++));

        if (remarks != null) {

          setBeanValue(object, _DataObjectProperty.Remarks, remarks);
        }

        Object packageName = null;

        packageName = resultSet.getObject(columnIndex++);

        object.setPackageNameNull(resultSet.getBoolean(columnIndex++));

        if (packageName != null) {

          setBeanValue(object, _DataObjectProperty.PackageName, packageName);
        }

        Object domainId = null;

        domainId = resultSet.getObject(columnIndex++);

        object.setDomainIdNull(resultSet.getBoolean(columnIndex++));

        if (domainId != null) {

          setBeanValue(object, _DataObjectProperty.DomainId, domainId);
        }

        Object domainName = null;

        domainName = resultSet.getObject(columnIndex++);

        object.setDomainNameNull(resultSet.getBoolean(columnIndex++));

        if (domainName != null) {

          setBeanValue(object, _DataObjectProperty.DomainName, domainName);
        }

        return object;
      } catch (Exception e) {
        throw new SQLException(e.getMessage());
      }
    }
  }
}
